package com.hsource.doctorcenter.service.order.impl;

import com.alibaba.fastjson.JSONObject;
import com.hsource.doctorcenter.domain.doctor.DoctorPrice;
import com.hsource.doctorcenter.domain.order.Order;
import com.hsource.doctorcenter.domain.order.OrderDoctor;
import com.hsource.doctorcenter.service.api.IApiService;
import com.hsource.doctorcenter.service.doctor.IDoctorPriceService;
import com.hsource.doctorcenter.service.order.IOrderDoctorService;
import com.hsource.doctorcenter.util.SpringUtils;
import com.hsource.doctorcenter.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/9/30 15:35
 * @describe 定向图文服务逻辑
 */
@Slf4j
public class DirectedPhoneServiceImpl {
    /**
     * 开通服务
     * @param order
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    public void openingService(Order order) throws ParseException, NoSuchAlgorithmException {
        IApiService apiService = SpringUtils.getBean(IApiService.class);
        IOrderDoctorService oderDoctorService = SpringUtils.getBean(IOrderDoctorService.class);
        IDoctorPriceService doctorPriceService = SpringUtils.getBean(IDoctorPriceService.class);
        List<OrderDoctor> pidDoctors = order.getOrderDoctors().stream().filter(od -> !od.getDoctorId().equals("0")).collect(Collectors.toList());
        if(StringUtils.isNotNull(pidDoctors)&&pidDoctors.stream().count() > 0) {
            String collect = pidDoctors.stream().map(od -> od.getDoctorId()).collect(Collectors.joining("#"));
            //医生原始价格
            Long[] originalPriceToral={0L};
            //查询医生原始价格
            pidDoctors.stream().forEach(d->{
                DoctorPrice dp=new DoctorPrice();
                dp.setDoctorId(d.getDoctorId());
                dp.setConsultType("TH");
                dp.setTimeLength(d.getDuration());
                dp=doctorPriceService.selectDoctorPriceList(dp).stream().findFirst().get();
                originalPriceToral[0]=originalPriceToral[0]+(StringUtils.isNotNull(dp)?dp.getOriginalPrice():0L);
            });
            String orientedProblem = apiService.createOrientedOrder(order.getUserId(), collect, order.getContent(), originalPriceToral[0].intValue()/100, order.getId(), order.getPhone(), order.getReservationTime(), new Long(order.getDuration()).intValue());
            log.info("=====================DirectedPhoneServiceImpl=服务======"+orientedProblem);
            JSONObject jsonObject = JSONObject.parseObject(orientedProblem);
            if(jsonObject.get("error").toString().equals("0")){
                try{
                    OrderDoctor od=new OrderDoctor();
                    od.setDoctorId(order.getOrderDoctors().get(0).getDoctorId());
                    od.setProblemId(jsonObject.getInteger("service_id"));
                    od.setOrderId(order.getId());
                    od.setInquiryTime(jsonObject.getString("inquiry_time"));
                    oderDoctorService.updateOrderDoctorProblemIdByDoctorId(od);
                }catch (Exception e){
                    log.error("创建定向电话问题解析异常:");
                }
            }
        }
    }
}
